﻿using System;

namespace CodeFirstConsoleApplication1
{
    using System.Data.Entity;

    class Program
    {
        static void Main(string[] args)
        {
            Save();
            Update();
            Delete();
        }

        public static void Save()
        {
            using (var content = new TinkercContext())
            {
                content.Users.Add(new User()
                {
                    Id = 10001,
                    Mobile = "18501685825",
                    ModifyTime = DateTime.Now,
                    UserName = "张三",
                    ValId = "T"
                });

                content.SaveChanges();
            }
        }

        public static void Update()
        {
            using (var content = new TinkercContext())
            {
                var entity = new User()
                {
                    Id = 1,
                    Mobile = "18501685825",
                    ModifyTime = DateTime.Now,
                    UserName = "李四",
                    ValId = "T"
                };

                content.Users.Attach(entity);//附加到上下文
                content.Entry(entity).State = EntityState.Modified;//修改State

                content.SaveChanges();
            }
        }

        public static void Delete()
        {
            using (var content = new TinkercContext())
            {
                var entity = new User()
                {
                    Id = 1
                };

                content.Entry(entity).State = EntityState.Deleted;

                content.SaveChanges();
            }
        }
    }
}
